var login={
	data:{
		username:null,
		password:null
	},
	validate:function(){
		login.data.username=$("#username").val();
		login.data.password=$("#password").val();
		if($.trim(login.data.username)=="")
		{
			$("#error").text("用户名或密码不能为空");
			$("#username").focus();
			return false;
		}
		else if($.trim(login.data.password)=="")
		{
			$("#error").text("用户名或密码不能为空");
			$("#password").focus();
			return false;
		}
		$("#error").text("");
		return true;
	},
	doLogin:function(){
		if(login.validate())
		{
			$.ajax({
				url:WEB_ROOT+"/login/login.action",
				dataType:"json",
				data:login.data,
				type:"POST",
				//timeout:10000,
				success:function(data){
					if(data.status==4)
					{
						location.href=WEB_ROOT+"/index/index.action";
						return;
					}
					else
					{
						$("#error").text(data.msg);
					}
				},
				error:function(error){
					alert("网络错误");
				}
			})
		}
		
	},
	initEvents:function(){
		$("#username").focus();
		$("#loginBtn").unbind("click" );
		$("#loginBtn").bind("click",function(){
			console.info("logon...");
			login.doLogin();
		});
		//得到焦点
		$("#password").focus(function(){
			$("#left_hand").animate({
				left: "150",
				top: " -38"
			},{step: function(){
				if(parseInt($("#left_hand").css("left"))>140){
					$("#left_hand").attr("class","left_hand");
				}
			}}, 2000);
			$("#right_hand").animate({
				right: "-64",
				top: "-38px"
			},{step: function(){
				if(parseInt($("#right_hand").css("right"))> -70){
					$("#right_hand").attr("class","right_hand");
				}
			}}, 2000);
		});
		//失去焦点
		$("#password").blur(function(){
			$("#left_hand").attr("class","initial_left_hand");
			$("#left_hand").attr("style","left:100px;top:-12px;");
			$("#right_hand").attr("class","initial_right_hand");
			$("#right_hand").attr("style","right:-112px;top:-12px");
		});
		
		$(window).bind("keydown",function(event){
			if(event.keyCode==13){
				
				var operatorElementId=$( document.activeElement ).attr("id");
				if(operatorElementId=="username")
						$("#password").focus();
				else if(operatorElementId=="password")
					login.doLogin();
			}
		});
	}
		
}

$(function(){
	login.initEvents();
});